package com.atguigu.async;

import com.atguigu.exception.SleepUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class FutureDemo2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        runAsync();
        System.out.println("main");
        SleepUtils.sleep(3);
    }

    public static void runAsync(){
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"你好runAsync");
                int a=1/0;
            }
        }).whenComplete(new BiConsumer<Void, Throwable>() {
            @Override
            public void accept(Void acceptVoid, Throwable throwable) {
                System.out.println("异步执行后接收的值"+acceptVoid);
                System.out.println("遇到的异常"+throwable);
            }
        }).exceptionally(new Function<Throwable, Void>() {
            @Override
            public Void apply(Throwable throwable) {
                System.out.println("exceptionally遇到的异常"+throwable);
                return null;
            }
        });
    }
}
